package com.eboss.sbs.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ScrollView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.TabBasicActivity;
import com.eboss.sbs.adapter.MessageListAdapter;
import com.eboss.sbs.config.Config;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.MessageEntity;
import com.eboss.sbs.model.MessageListEntity;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mytools.tool.views.swipemenulistview.SwipeMenu;
import com.mytools.tool.views.swipemenulistview.SwipeMenuCreator;
import com.mytools.tool.views.swipemenulistview.SwipeMenuItem;
import com.mytools.tool.views.swipemenulistview.SwipeMenuListView;
import com.mytools.tool.views.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageActivity extends TabBasicActivity implements OnItemClickListener {

	private PullToRefreshScrollView pullToRefreshScrollView;
	
	private SwipeMenuListView listview;
	private SwipeMenuCreator creator;
	
	private MessageListAdapter adapter;
	private List<MessageEntity> datas = new ArrayList<MessageEntity>();
	
	private int page = 1;
	private int rows = Config.ROWS;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_message);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, false);
		setTvText(R.id.tv_title, "消息通知");
//		showView(R.id.next_sure, true);
//		setTvText(R.id.next_sure, "编辑");
		
		pullToRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pullToRefreshScrollView);
		pullToRefreshScrollView.setOnRefreshListener(new MyOnRefreshListener());
		pullToRefreshScrollView.setMode(Mode.BOTH);
		
		listview = (SwipeMenuListView)findViewById(R.id.listview);
		listview.setOnItemClickListener(this);
		creator = new SwipeMenuCreator() {
			@Override
			public void create(SwipeMenu menu) {
				SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
				deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
				deleteItem.setWidth(mApp.dp2px(90));
				deleteItem.setIcon(R.mipmap.ic_delete);
				menu.addMenuItem(deleteItem);
			}
		};
		listview.setMenuCreator(creator);
		listview.setOnMenuItemClickListener(new SwipeMenuClickEvent());
		
		adapter = new MessageListAdapter(this, datas);
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		findViewById(R.id.next_sure).setOnClickListener(this);
		
		listview.setAdapter(adapter);
		
		getdata();
		
	}
	
	class SwipeMenuClickEvent implements OnMenuItemClickListener{
		@Override
		public void onMenuItemClick(int position, SwipeMenu menu, int index) {
			switch (index) {
				case 0:
					deleteMsg(position);
					break;
				default: break;
			}
		}
	}
	
	class MyOnRefreshListener implements OnRefreshListener2<ScrollView> {
		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
			pullToRefreshScrollView.setMode(Mode.BOTH);
			String label = DateUtils.formatDateTime(mActivity, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
			refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
			refreshData();
		}
		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
			page++;
			getdata();
		}
	}
	
	private void refreshData(){
		datas.clear();
		page = 1;
		getdata();
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("pagenum", page);
		params.put("pagesize", rows);
		
		API.http_post(this, API.MESSAGE_LIST, params, new HttpCallback<MessageListEntity>(this) {
			@Override
			public void onSuccess(int statusCode, MessageListEntity data) {
				
				if(data!=null){
					 List<MessageEntity> tmp_datas = data.getMsgs();
					 if(tmp_datas==null||tmp_datas.size()==0||tmp_datas.size()<rows){
						if(tmp_datas==null||tmp_datas.size()==0){
							toast("没有更多数据");
						}
						pullToRefreshScrollView.setMode(Mode.PULL_DOWN_TO_REFRESH);
					 }
					 if(tmp_datas!=null&&tmp_datas.size()>0){
						 datas.addAll(tmp_datas);
					 }
				}
				boolean no_datas = (datas==null||datas.size()==0);
				showView(R.id.eva_nodata, no_datas);
				showView(R.id.listview, !no_datas);
				adapter.notifyDataSetChanged();
				pullToRefreshScrollView.onRefreshComplete();
				
			}
			
			@Override
			public void onFailure(int statusCode, String message) {
				adapter.notifyDataSetChanged();
				pullToRefreshScrollView.onRefreshComplete();
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<MessageListEntity>() {
				};
			}
		});
	}
	
	private void deleteMsg(final int position){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("msgId", datas.get(position).getId());
		
		API.http_post(this, API.MESSAGE_DELETE, params, new HttpCallback<MessageListEntity>(this) {
			@Override
			public void onSuccess(int statusCode, MessageListEntity data) {
				datas.remove(position);
				adapter.notifyDataSetChanged();
			}
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<MessageListEntity>() {
				};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.next_sure:
				
				break;
			default:break;
		}
		super.onClick(v);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(mActivity, MessageDetailActivity.class);
		intent.putExtra(MessageDetailActivity.MSG_ID, datas.get(position).getId());
		startActivity(intent);
	}
	
}
